/* =========================================================================
 *
 *   The traffic processor.
 *
 *   Development by Sedelnikov Alexander (Bigking).
 *
 * =========================================================================
 */

#ifndef __COMMON_H__
#define __COMMON_H__


#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <exception>
#include <vector>
#include <string>
#include <algorithm>


typedef unsigned long  ulong;
typedef unsigned short ushort;
typedef unsigned int   uint;


//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
//-------------------------------------------------------------------------//
//
// The main exception class.
//
class cexception : public std::exception
{


protected:

    //-----------------------------------------------------------------------//
    char     message[ 256 ],
            *exception_label,
            *file,
            *function;
    int      line;

    char     buffer[ 256 ];


public:

    //-----------------------------------------------------------------------//
    cexception( );
    cexception( const char *fl,
                const char *exc_lbl,
                const char *func,
                const int   l );

    void        set_message( const char *fmt, ... );

    const char* get_message() const;
    const char* get_file() const;
    const char* get_exception_name() const;
    const int   get_line() const;
    const char* get_function() const;

    const char* get_formated_message( );


};


//-----------------------------------------------------------------------//
#define EXCEPTION(name) class name : public ::cexception \
                        { \
                          public: \
                              name() : cexception() \
                              {    } \
                              name(  \
                                    const char *fl, \
                                    const char *func, \
                                    const int   l ) : \
                                        cexception( fl, #name, func, l ) {    } \
                        }


#define THROW( class, ... )  { class _throw_obj( __FILE__, __PRETTY_FUNCTION__, __LINE__ ); \
                               _throw_obj.set_message( __VA_ARGS__ ); \
                               throw _throw_obj; }


//-------------------------------------------------------------------------//
//
// Files operations
//
bool  file_exist( const char *fname );
ulong file_size ( const char *fname );
bool  file_extension_check( const char *fname, const char *mask );
char *file_basename( const char *fname, char *out, int max_len );
char *file_extension( const char *fname, char *out, int max_len );
char *file_path( const char *fname, char *out, int max_len );
void  file_copy( const char *src, const char *dst );


EXCEPTION( e_file_read );
EXCEPTION( e_file_write );


#endif
